package com.data.modules.jd.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.data.common.util.HttpClientUtil;
import com.data.common.util.WrtPushUtils;
import com.data.modules.jd.entity.JdCarInfo;
import com.data.modules.jd.entity.JdPackInfo;
import com.data.modules.jd.mapper.JdCarInfoMapper;
import com.data.modules.jd.service.IJdCarInfoService;
import com.data.modules.jd.service.IJdPackInfoService;
import com.data.modules.jd.vo.BasePackInfo;
import com.data.modules.jd.vo.BaseResult;
import com.data.modules.jd.vo.BaseResultInfo;
import com.data.modules.wrt.entity.WrtRoom;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 金地车辆进出信息
 * @Author: data-boot
 * @Date:   2023-09-05
 * @Version: V1.0
 */
@Service
@Slf4j
public class JdCarInfoServiceImpl extends ServiceImpl<JdCarInfoMapper, JdCarInfo> implements IJdCarInfoService {

    @Autowired
    private JdCarInfoMapper mapper;
    @Autowired
    private IJdPackInfoService packInfoService;
    @Override
    public Map queryCarInfo() {
        Map<String,Object> map = mapper.searchCarInfo();
        map.putAll(mapper.searchYestayCarInfo());
        return map;
    }

    @Override
    public void synParckInfo(String parameter) {

        String url = "http://192.168.13.8:8089/sp/login";
        String baseUrl="http://192.168.13.8:8089/sp/parkinglot/getBasicInfo";
        //大华停车场验证
        Map<String,Object> map = new HashMap<>();
        map.put("method","/sp/login");
        map.put("sessionId","1ccc");
        Map<String,Object> newMap = new HashMap<>();
        newMap.put("userName","admin");
        newMap.put("passWord","374B27EEDC50C531159DDEEC6E03882E");
        map.put("info",newMap);
        String str = JSON.toJSONString(map);
        String result = HttpClientUtil.doPostJson(url, str);
        BaseResult base = JSON.toJavaObject(JSON.parseObject(result),BaseResult.class);

        if(ObjectUtil.isNotEmpty(base) && ObjectUtil.equals(200,base.getCode())){

            BaseResultInfo basePackInfo = JSON.toJavaObject(JSON.parseObject(base.getInfo()),BaseResultInfo.class);
            String accessToken = basePackInfo.getData().get(0).getAccessToken();

            Map baseMap = new HashMap();
            baseMap.put("method","/sp/parkinglot/getBasicInfo");
            baseMap.put("sessionId",accessToken);

            String resultPackInfo = WrtPushUtils.sendWrtPostRequest(baseUrl, JSON.toJSONString(baseMap),"UTF-8",accessToken);
            if(ObjectUtil.isNotEmpty(resultPackInfo)){
                BaseResult packInfo = JSON.toJavaObject(JSON.parseObject(resultPackInfo),BaseResult.class);
                if(ObjectUtil.isNotEmpty(packInfo) && ObjectUtil.equals(200,packInfo.getCode())){

                    BasePackInfo pack = JSON.toJavaObject(JSON.parseObject(packInfo.getInfo()),BasePackInfo.class);
                    JdPackInfo jdPackInfo =pack.getData().get(0);
                    packInfoService.save(jdPackInfo);
                }
            }
         }
    }
}
